package com.yikaiye.android.yikaiye.di.retrofit;

import com.yikaiye.android.yikaiye.model.AlipayModel;
import com.yikaiye.android.yikaiye.model.BannerModel;
import com.yikaiye.android.yikaiye.model.CheckComboResponseModel;
import com.yikaiye.android.yikaiye.model.CommitOrderResultModel;
import com.yikaiye.android.yikaiye.model.CompanyRecordsModel;
import com.yikaiye.android.yikaiye.model.CustomerServiceModel;
import com.yikaiye.android.yikaiye.model.FddModel;
import com.yikaiye.android.yikaiye.model.HomeIconStatusModel;
import com.yikaiye.android.yikaiye.model.InvoiceDetailsModel;
import com.yikaiye.android.yikaiye.model.InvoiceRecordsModel;
import com.yikaiye.android.yikaiye.model.MessageRecordsModel;
import com.yikaiye.android.yikaiye.model.OrderDataModel;
import com.yikaiye.android.yikaiye.model.OrderProductModel;
import com.yikaiye.android.yikaiye.model.OrderProgressModel;
import com.yikaiye.android.yikaiye.model.ProductDataModel;
import com.yikaiye.android.yikaiye.model.ProductDetailDataModel;
import com.yikaiye.android.yikaiye.model.ServiceDetailsModel;
import com.yikaiye.android.yikaiye.model.ServiceRecordsModel;
import com.yikaiye.android.yikaiye.model.TypeModel;
import com.yikaiye.android.yikaiye.model.UserModel;
import com.yikaiye.android.yikaiye.model.VerifyCodeModel;
import com.yikaiye.android.yikaiye.model.VersionModel;
import com.yikaiye.android.yikaiye.model.company.CompanyIntroModel;
import com.yikaiye.android.yikaiye.model.company.GsBaseInfoModel;
import com.yikaiye.android.yikaiye.model.company.GsChangeRecordDataModel;
import com.yikaiye.android.yikaiye.pay.WeChatInfoModel;

import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.FlowableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class RetrofitHelper {
    private final ApiConstants mApiConstants;

    public RetrofitHelper(ApiConstants paramApiConstants) {
        this.mApiConstants = paramApiConstants;
    }

    public static <T> FlowableTransformer<T, T> appointThread() {
        return upstream -> upstream.subscribeOn(Schedulers.io())
                // 使用observerOn()指定订阅者运行的线程
                .observeOn(AndroidSchedulers.mainThread());
    }

    /**
     * 版本更新
     */
    public Flowable<BaseModel<VersionModel>> checkVersion() {
        return mApiConstants.checkVersion().compose(appointThread());
    }

    /**
     * 查询预订单
     */
    public Flowable<BaseModel<OrderDataModel>> getOrderList() {
        return mApiConstants.getOrderList().compose(appointThread());
    }

    /**
     * 获取用户专属客服
     */
    public Flowable<BaseModel<List<CustomerServiceModel>>> getCustomerService() {
        return mApiConstants.getCustomerService().compose(appointThread());
    }

    /**
     * 产品分类
     */
    public Flowable<BaseModel<TypeModel>> getTypeList(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.getTypeList(requestBody).compose(appointThread());
    }

    /**
     * 产品分类
     */
    public Flowable<BaseModel<ProductDataModel>> getProductList(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.getProductList(requestBody).compose(appointThread());
    }

    /**
     * 首页开票入口状态
     */
    public Flowable<BaseModel<HomeIconStatusModel>> getHomeIconStatus() {
        return mApiConstants.getHomeIconStatus().compose(appointThread());
    }

    /**
     * 个人信息
     */
    public Flowable<BaseModel<UserModel>> getUserInfo(String id) {
        return mApiConstants.getUserInfo(id).compose(appointThread());
    }

    /**
     * 订单列表
     */
    public Flowable<BaseModel<OrderDataModel>> getOrderList(String status, int page, int pageSize) {
        return mApiConstants.getOrderList(status, page, pageSize).compose(appointThread());
    }

    /**
     * 取消订单
     */
    public Flowable<BaseModel<String>> cancelOrder(String id) {
        return mApiConstants.cancelOrder(id).compose(appointThread());
    }

    /**
     * 取消订单
     */
    public Flowable<BaseModel<FddModel>> lookView(String orderNo) {
        return mApiConstants.lookView(orderNo).compose(appointThread());
    }

    /**
     * 查看认证结果
     */
    public Flowable<BaseModel<FddModel>> queryVerifyResult(String orderNo, int customerType) {
        return mApiConstants.queryVerifyResult(orderNo, customerType).compose(appointThread());
    }

    /**
     * 签署协议
     */
    public Flowable<BaseModel<FddModel>> signContract(String orderNo, int customerType) {
        return mApiConstants.signContract(orderNo, customerType).compose(appointThread());
    }

    /**
     * 实名认证
     */
    public Flowable<BaseModel<FddModel>> realNameVerify(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.realNameVerify(requestBody).compose(appointThread());
    }

    /**
     * 重新实名认证
     */
    public Flowable<BaseModel<FddModel>> resetVerify(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.resetVerify(requestBody).compose(appointThread());
    }

    /**
     * 提交订单
     */
    public Flowable<BaseModel<CommitOrderResultModel>> commitOrder(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.commitOrder(requestBody).compose(appointThread());
    }

    /**
     * 订单详情
     */
    public Flowable<BaseModel<OrderProductModel>> getOrderDetails(String id) {
        return mApiConstants.getOrderDetails(id).compose(appointThread());
    }

    /**
     * 微信预支付接口
     */
    public Flowable<BaseModel<WeChatInfoModel>> getWeChatPay(String id, String payPlatform, String payCode, String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.getWeChatPay(id, payPlatform, payCode, requestBody).compose(appointThread());
    }

    /**
     * 支付宝接口
     */
    public Flowable<BaseModel<AlipayModel>> getAliPay(String id, String payPlatform, String payCode, String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.getAliPay(id, payPlatform, payCode, requestBody).compose(appointThread());
    }

    /**
     * 产品详情
     */
    public Flowable<BaseModel<ProductDetailDataModel>> getProductDetails(String id) {
        return mApiConstants.getProductDetails(id).compose(appointThread());
    }

    /**
     * 首页轮播图
     */
    public Flowable<BaseModel<List<BannerModel>>> getBanner(int showPosition) {
        return mApiConstants.getBanner(showPosition).compose(appointThread());
    }

//    /**
//     * 消息列表
//     */
//    public Flowable<BaseModel<List<MessageRecordsModel.MessageModel>>> getMessageList(String userId) {
//        return mApiConstants.getMessageList(userId).compose(appointThread());
//    }

    /**
     * 消息列表(新)
     */
    public Flowable<BaseModel<MessageRecordsModel>> getMessageList(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.getMessageList(requestBody).compose(appointThread());
    }

    /**
     * 全部标为已读
     */
    public Flowable<BaseModel<String>> allReady(String userId) {
        return mApiConstants.allReady(userId).compose(appointThread());
    }

    /**
     * 删除消息
     */
    public Flowable<BaseModel<String>> deleteMessage(String id) {
        return mApiConstants.deleteMessage(id).compose(appointThread());
    }

    /**
     * 上传图片
     */
    public Flowable<BaseModel<String>> uploadImage(MultipartBody.Part part) {
        return mApiConstants.uploadImage(part).compose(appointThread());
    }

    /**
     * 修改个人信息
     */
    public Flowable<BaseModel<String>> updateUserInfo(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.updateUserInfo(requestBody).compose(appointThread());
    }

    /**
     * 工商信息 基本信息
     */
    public Flowable<BaseModel<GsBaseInfoModel>> getGsBaseInfo(String eid) {
        return mApiConstants.getGsBaseInfo(eid).compose(appointThread());
    }

    /**
     * 工商变更记录
     */
    public Flowable<BaseModel<GsChangeRecordDataModel>> getGsChangeRecord(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.getGsChangeRecord(requestBody).compose(appointThread());
    }

    /**
     * 发送账号合并验证码
     */
    public Flowable<BaseModel<String>> sendSmsCode(String phone, String userId) {
        return mApiConstants.sendSmsCode(phone, userId).compose(appointThread());
    }

    /**
     * 提交绑定合并
     */
    public Flowable<BaseModel<String>> bindingPhone(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.bindingPhone(requestBody).compose(appointThread());
    }

    /**
     * 服务列表
     */
    public Flowable<BaseModel<ServiceRecordsModel>> getServiceList(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.getServiceList(requestBody).compose(appointThread());
    }

    /**
     * 获取服务详情
     */
    public Flowable<BaseModel<ServiceDetailsModel>> getServiceDetails(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.getServiceDetails(requestBody).compose(appointThread());
    }

    /**
     * 查找企业详情
     */
    public Flowable<BaseModel<CompanyIntroModel>> getCompanyDetail(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.getCompanyDetail(requestBody).compose(appointThread());
    }

    /**
     * 发票列表
     */
    public Flowable<BaseModel<InvoiceRecordsModel>> getInvoiceList(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.getInvoiceList(requestBody).compose(appointThread());
    }

    /**
     * 发票详情
     */
    public Flowable<BaseModel<InvoiceDetailsModel>> getInvoiceDetails(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.getInvoiceDetails(requestBody).compose(appointThread());
    }

    /**
     * 查找我的企业
     */
    public Flowable<BaseModel<CompanyRecordsModel>> getMyCompanyList(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.getMyCompanyList(requestBody).compose(appointThread());
    }

    /**
     * 查询企业关联服务
     */
    public Flowable<BaseModel<List<ServiceRecordsModel.ServiceModel>>> getServiceByCreditNo(String creditNo) {
        return mApiConstants.getServiceByCreditNo(creditNo).compose(appointThread());
    }

    /**
     * 查询企业关联服务
     */
    public Flowable<BaseModel<List<OrderProgressModel>>> getServiceByOrderId(String orderId) {
        return mApiConstants.getServiceByOrderId(orderId).compose(appointThread());
    }

    /**
     * 检查是否存在此套餐
     */
    public Flowable<BaseModel<CheckComboResponseModel>> checkProduct(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.checkProduct(requestBody).compose(appointThread());
    }

    /**
     * 获取验证码
     */
    public Flowable<BaseModel<VerifyCodeModel>> sendCode(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.sendCode(requestBody).compose(appointThread());
    }

    /**
     * 伪token
     */
    public Flowable<BaseModel<UserModel>> verifyCode(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.verifyCode(requestBody).compose(appointThread());
    }

    /**
     * 验证验证码(登录)
     */
    public Flowable<BaseModel<UserModel>> login(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.login(requestBody).compose(appointThread());
    }

    /**
     * 绑定手机号
     */
    public Flowable<BaseModel<UserModel>> bindPhone(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.bindPhone(requestBody).compose(appointThread());
    }

    /**
     * 重置密码
     */
    public Flowable<BaseModel<UserModel>> resetPassword(String json) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), json);
        return mApiConstants.resetPassword(requestBody).compose(appointThread());
    }
}
